//
//  LoginViewController.m
//  联付商圈
//
//  Created by 韩佳睿 on 2017/8/16.
//  Copyright © 2017年 lianfu. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "YYModel.h"
#import "ModifyPasswordViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation LoginViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.observeKeyboard = YES;
    self.phoneTextField.delegate = self;
    self.passwordTextField.delegate = self;
}

- (IBAction)backBtnAction:(id)sender {
    [self popViewController];
}

- (IBAction)loginBtnAction:(id)sender {
    
    if (!([NSString isNilOrEmpty:_phoneTextField.text] || [NSString isNilOrEmpty:_passwordTextField.text])) {
        NSDictionary *param = @{@"name" : _phoneTextField.text, @"sfzh" : _passwordTextField.text, @"token" : @"220QWER181ASD1994ZXC"};
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
        
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.securityPolicy.allowInvalidCertificates = YES;
        
        [manager POST:[Host stringByAppendingString:@"shoploginout?"] parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
            if (dic[@"success"]) {
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                [userDefaults setObject:dic[@"success"] forKey:@"userInfo"];
                [userDefaults setObject:_passwordTextField.text forKey:@"password"];
                [userDefaults synchronize];
                
//                MainTabBarController *tabBar = [MainTabBarController new];
//                NSString *state = dic[@"success"][@"state"];
//                tabBar.userType = state.integerValue;
//                tabBar.userModel = [SQUserModel yy_modelWithDictionary:dic[@"success"]];
//                [UIApplication sharedApplication].delegate.window.rootViewController = tabBar;
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = dic[@"error"][@"message"];
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    } else {
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
        HUD.labelText = @"请输入手机号码或登录密码";
        HUD.mode = MBProgressHUDModeText;
        [HUD hide:YES afterDelay:1.0];
    }
}

- (IBAction)forgetPasswordBtnAction:(id)sender {
    ModifyPasswordViewController *vc = [ModifyPasswordViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)registerBtnAction:(id)sender {
    RegisterViewController *vc = [RegisterViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    [super keyboardWillShow:notification];
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect frame = self.view.frame;
    frame.origin.y = -keyboardFrame.size.height+(self.view.sizeH-_loginBtn.originY-_loginBtn.sizeH);
    self.view.frame = frame;
}

- (void)keyboardWillHide:(NSNotification *)notification {
    [super keyboardWillHide:notification];
    CGRect frame = self.view.frame;
    frame.origin.y = 0;
    self.view.frame = frame;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return true;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
